package com.example.newspublish.controller;

import com.example.newspublish.pojo.entity.SysRole;
import com.example.newspublish.pojo.result.Result;
import com.example.newspublish.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name="角色管理")
@CrossOrigin
@RestController
@RequestMapping("/news")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Operation(summary = "获取用户角色数据", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("toAssign/{userId}")
    public Result toAssign(@PathVariable String userId) {
        Map<String,Object> roleMap = sysRoleService.getRolesByUserId(userId);
        return Result.ok(roleMap);
    }

    @Operation(summary = "批量删除", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("roleBatchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        sysRoleService.removeByIds(ids);
        return Result.ok();
    }

    @Operation(summary = "修改", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("sysRoleUpdate")
    public Result updateRole(@RequestBody SysRole sysRole) {
        boolean isSuccess = sysRoleService.updateById(sysRole);
        if(isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Operation(summary = "根据id查询", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("findRoleById/{id}")
    public Result findRoleById(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    @Operation(summary = "添加角色", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("roleSave")
    public Result saveRole(@RequestBody SysRole sysRole) {
        boolean isSuccess = sysRoleService.save(sysRole);
        if(isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    @Operation(summary = "查询所有记录", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("findAllRole")
    public Result findAllRole() {
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }





}
